package com.shujia.core

import org.apache.flink.streaming.api.scala._

object Demo1WordCount {
  def main(args: Array[String]): Unit = {

    //创建flink的环境

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    //设置并行度
    env.setParallelism(2)

    //读取socker数据
    //nc -lk 8888

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    //将单词拆分出来
    val wordsDS: DataStream[String] = linesDS.flatMap(_.split(","))

    //转换成kv格式
    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))


    //安装单词分组

    val keyByDS: KeyedStream[(String, Int), String] = kvDS.keyBy(_._1)


    //统计单词的数量
    /**
      * sum算子内部市有状态计算，累加统计
      */
    val countDS: DataStream[(String, Int)] = keyByDS.sum(1)

    //打印结果

    countDS.print()



    //启动flink
    env.execute()


  }

}
